<%- include('../layout/header.ejs') %>

  <link rel="stylesheet" href="/node_modules/mditor/dist/css/mditor.min.css">
  <script src="/node_modules/mditor/dist/js/mditor.min.js"></script>

  <div class="container">
    <h1>编辑文章页</h1>
    <hr>
    <form id="form">
      <!--应该把文章的标题，作为 隐藏域，保存到 表单中-->
      <input type="hidden" name="id" value="<%= article.id %>">
      <div class="form-group">
        <label>文章标题：</label>
        <input type="text" name="title" class="form-control" required value="<%= article.title %>">
      </div>

      <div class="form-group">
        <label>文章内容：</label>
        <textarea name="content" class="form-control" id="editor"><%= article.content %></textarea>
      </div>

      <div class="form-group">
        <input type="submit" value="保存文章" class="btn btn-primary">
      </div>
    </form>
  </div>

  <script>
    $(function(){
      let mditor = Mditor.fromTextarea(document.getElementById('editor'));
      //点击保存文章按钮发送ajax请求更新编辑后的内容
      $('#form').on('submit',function(e){
          //阻止submit的默认提交功能
          e.preventDefault();
          //发送ajax
          $.ajax({
            type: "post",
            url: "/article/editArticle",
            data: $('#form').serialize(),
            success: function (res) {
              // console.log(res);
              if(res.status == 200){
                setTimeout(function(){
                  location.href = '/article/info/' + res.id;
                },1000)
              }else{
                alert(res.message);
              }
            }
          });
      })
    })
     
  </script>

  <%- include('../layout/footer.ejs') %>